<?php
class Polls_m extends MY_Model{

	private $select;
	private $_table_lang;
	
	function __construct(){
		parent::__construct();
		$this->select = 'c.id,c.created,c.ordering,l.title';
		//$this->_table = 'polls_categories';
	}
	
	function vote($id){
		$this->_table = 'polls_answer';
		$this->db->where('id',$id);
		$obj = parent::get($id)->votes;
		
		$this->db->where('id',$id);
		$this->db->update($this->_table,array('votes'=>$obj + 1));
	}
	function block(){
		$this->_table = 'polls_categories c';
		$this->_table_lang = 'polls_categories_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang, 'c.id = l.id');
		$this->db->where('c.ordering',0);
		$this->db->where('l.lang',$this->_lang);
		$this->db->limit(1);
		$cat = $this->db->get($this->_table)->row();
		$model = new stdClass();
		if(!empty($cat)){
			$model->title = $cat->title;
			
			$this->_table = 'polls_answer';
			$this->db->where('cat_id',$cat->id);
			$this->db->order_by('ordering');
			$this->db->where('lang',$this->_lang);
			$model->answers = parent::get_all();
		}
		//print_r($model);
		//$this->session->set_userdata('polls',NULL);
		$polls_vote = $this->session->userdata('polls');
		return $this->template
			->set_layout(FALSE)
			->set('model',$model)
			->set('polls_vote',$polls_vote)
			->build('polls/block');
		//return $this->output->get_output();
	}
	
}